#!/bin/bash

set -e

rm -rf bin
rm -rf tmp
mkdir tmp
cleanup_exit() {
  echo "Test failed" ${2}
  exit ${1}
}

echo "Build should fail"
../../build.py build :a > tmp/output 2>&1 && cleanup_exit 1 "build :a #1 succeded"

# the target which triggered the problem should be in the output, along with
# the BUILD file and the line number.
cat tmp/output
cat tmp/output | grep '//:a' || cleanup_exit 1 'bad output.'
cat tmp/output | grep 'BUILD' || cleanup_exit 1 'bad output.'
cat tmp/output | grep '4' || cleanup_exit 1 'bad output.'


echo "Build should fail"
../../build.py build //test:a > tmp/output 2>&1 && cleanup_exit 1 "build test:a #2 succeded"

# the target which triggered the problem should be in the output, along with
# the BUILD file and the line number.
cat tmp/output
cat tmp/output | grep '//test:a' || cleanup_exit 1 'bad output.'
cat tmp/output | grep 'BUILD' || cleanup_exit 1 'bad output.'
cat tmp/output | grep '4' || cleanup_exit 1 'bad output.'

rm -rf bin
rm -rf tmp

echo 'Test passed'
